<?php
/**
 * Created by JetBrains PhpStorm.
 * User: zhouyang
 * Date: 13-9-06
 * Time: 下午4:31
 * To change this template use File | Settings | File Templates.
 */
class SummarysDB  extends DBReader{

    //根据summary_id 获取summary 信息
    public function get($summary_id){

        $sql = 'select summary_id, source_id, title, status, summary, published, update_time, content, url, user_id from summarys where summary_id = :summary_id';

        return $this->db()->getRow($sql,array('summary_id'=>$summary_id));
    }

    //根据title 获取summary 信息
    public function getSummaryInfoByTitle($title){

        $sql = 'select summary_id, source_id, title, status, summary, published, update_time, content, url, user_id from summarys where title = :title';

        return $this->db()->getRow($sql,array('title'=>$title));
    }

    //获取摘要 列表<带分页>
    public function getSummaryList($curr_page,$per_page,$filter = array()){
        $where = ' 1=1 ';
        $bindarr = array();

        if(isset($filter['title'])){
            $where .= ' and title like :title';
            $bindarr['title'] = '%'.$filter['title'].'%';
        }

        if(isset($filter['status'])){
            $where .= ' and status = :status';
            $bindarr['status'] = $filter['status'];
        }

        if(isset($filter['source_id'])){
            $where .= ' and source_id = :source_id';
            $bindarr['source_id'] = $filter['source_id'];
        }

        if(isset($filter['p_stime'])){
            $where .= ' and published >= :p_stime';
            $bindarr['p_stime'] = $filter['p_stime'];
        }

        if(isset($filter['p_etime'])){
            $where .= ' and published <= :p_etime';
            $bindarr['p_etime'] = $filter['p_etime'];
        }

        if(isset($filter['u_stime'])){
            $where .= ' and update_time >= :u_stime';
            $bindarr['u_stime'] = $filter['u_stime'];
        }

        if(isset($filter['u_etime'])){
            $where .= ' and update_time <= :u_etime';
            $bindarr['u_etime'] = $filter['u_etime'];
        }

        $ret = $this->pager($curr_page, $per_page)
            ->ct('select count(summary_id) from summarys where ' . $where , $bindarr)
            ->rs('select summary_id, source_id, title, status, summary, published, update_time, content, url, user_id from summarys where '.$where.' order by summary_id desc',$bindarr);

        return $ret;
    }

    //添加资讯摘要信息
    public function addSummary($source_id, $title, $status = 1, $summary, $content, $url, $user_id, $published = null, $update_time = null){
        $sql = "insert into summarys(source_id, title, status, summary, published, update_time, content, url, user_id) values(:source_id, :title, :status, :summary, :published, :update_time, :content, :url, :user_id)";

        $bindarr = array(
            'source_id'=>$source_id,
            'title'=>$title,
            'status'=>$status,
            'summary'=>$summary,
            'published'=> empty($published) ? date('Y-m-d H:i:s') : $published,
            'update_time'=> empty($update_time) ? date('Y-m-d H:i:s') : $update_time,
            'content'=>$content,
            'url'=>$url,
            'user_id'=>$user_id,
        );
        $this->db()->execute($sql,$bindarr);
        return $this->db()->lastInsertId();
    }

    //删除摘要信息
    public function delSummary($summary_id){

        $sql = "delete from summarys where summary_id = :summary_id";

        $bindarr = array(
            'summary_id'=>$summary_id,
        );
        $rs = $this->db()->execute($sql,$bindarr);

        return $rs->rowCount();
    }

    //添加资讯摘要信息
    public function editSummary($summary_id, $source_id, $title, $status = 1, $summary, $content, $url){
        $sql = "update summarys set source_id = :source_id,title = :title,status = :status,summary = :summary,update_time = :update_time,content = :content,url = :url where summary_id = :summary_id";
        $bindarr = array(
            'summary_id' => $summary_id,
            'source_id'=>$source_id,
            'title'=>$title,
            'status'=>$status,
            'summary'=>$summary,
            'update_time'=>date('Y-m-d H:i:s'),
            'content'=>$content,
            'url'=>$url
        );
        return $this->db()->execute($sql,$bindarr);
    }

    //推荐资讯摘要
    public function recommendSummary($summary_id,$status){
        $sql = "update summarys set status = :status,published = :published,update_time = :update_time where summary_id = :summary_id";
        $bindarr = array(
            'summary_id' => $summary_id,
            'status'=>$status,
            'published'=>date('Y-m-d H:i:s'),
            'update_time'=>date('Y-m-d H:i:s')
        );
        return $this->db()->execute($sql,$bindarr);
    }
}